package com.example.mobifin_busticket.views.activities;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.example.mobifin_busticket.AppData;
import com.example.mobifin_busticket.BusTicketManagaer;
import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.R.color;
import com.example.mobifin_busticket.R.drawable;
import com.example.mobifin_busticket.R.id;
import com.example.mobifin_busticket.R.layout;
import com.example.mobifin_busticket.R.string;
import com.example.mobifin_busticket.controller.callbacks.ResponseListener;
import com.example.mobifin_busticket.models.CancellationTicketDetailsModel;
import com.example.mobifin_busticket.models.TicketDetailsModel;
import com.example.mobifin_busticket.views.dialogs.CancellationPolicy;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class CancelledSeatActivity extends MobifinActivity implements
		OnClickListener {
	TextView tvNewPNRNo, tvStatus, tvTotalFare, tvDepartureTime, tvTravelsName,
			tvToValue, tvFromValue, tvTravelContactNo, tvTicketNo,
			tvCancellationPOlicy, tvPartial, tvpassenger, tvpassengerage,
			tvpassengergender, tvseatno, tvticketStatus, tvCancellationCharge,
			tvRefundAmt;
	Button btn_year, btn_date, btn_dayName, btn_cancel, btn_headerBack;
	ScrollView scrlLayout;
	ArrayList<TicketDetailsModel> inventoryList;
	List<CheckBox> lstChekbox = null;
	ArrayList<String> passengerDetailsList, cancellationChargeList,
			refundFareList;
	LinearLayout llPassengerMain, llRoot, llPassengerCancelCharge;
	CheckBox chkPassenger;
	String strTicketNo, strcancellationPolicy, strDOJ;
	boolean strPartialcallationAllow;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cancelled_seat);
		tvNewPNRNo = (TextView) findViewById(R.id.tvNewPNRNo);
		tvStatus = (TextView) findViewById(R.id.tvStatus);
		tvTotalFare = (TextView) findViewById(R.id.tvTotalFare);
		tvDepartureTime = (TextView) findViewById(R.id.tvDepartureTime);
		tvTravelsName = (TextView) findViewById(R.id.tvTravelsName);
		tvTravelContactNo = (TextView) findViewById(R.id.tvTravelContactNo);
		tvToValue = (TextView) findViewById(R.id.tvToValue);
		tvFromValue = (TextView) findViewById(R.id.tvFromValue);
		tvTicketNo = (TextView) findViewById(R.id.tvTicketNo);
		tvCancellationPOlicy = (TextView) findViewById(R.id.tvCancellationPOlicy);
		tvCancellationCharge = (TextView) findViewById(R.id.tvCancellationCharge);
		tvRefundAmt = (TextView) findViewById(R.id.tvRefundAmt);
		llRoot = (LinearLayout) findViewById(R.id.llRoot);
		llPassengerCancelCharge = (LinearLayout) findViewById(R.id.llPassengerCancelCharge);
		tvPartial = (TextView) findViewById(R.id.tvPartial);
		btn_year = (Button) findViewById(R.id.btn_year);
		btn_date = (Button) findViewById(R.id.btn_date);
		btn_dayName = (Button) findViewById(R.id.btn_dayName);
		btn_cancel = (Button) findViewById(R.id.btn_cancel);
		scrlLayout = (ScrollView) findViewById(R.id.scrlLayout);
		scrlLayout.setVisibility(View.GONE);
		llPassengerCancelCharge.setVisibility(View.GONE);
		chkPassenger = (CheckBox) findViewById(R.id.chkPassenger);
		btn_cancel = (Button) findViewById(R.id.btn_cancel);
		btn_headerBack = (Button) findViewById(R.id.btn_headerBack);
		btn_headerBack.setOnClickListener(this);
		tvCancellationPOlicy.setOnClickListener(this);
		btn_cancel.setOnClickListener(this);
		passengerDetailsList = new ArrayList<String>();
		lstChekbox = new ArrayList<CheckBox>();
		cancellationChargeList = new ArrayList<String>();
		refundFareList = new ArrayList<String>();
		inventoryList = new ArrayList<TicketDetailsModel>();
		inventoryList = AppData.InventoryList;
		// setValueInView();
		strTicketNo = getIntent().getStringExtra("TicketNo");
		getTicketDetails();

	}

	private void getTicketDetails() {
		// TODO Auto-generated method stub
		ResponseListener ticketDetailsResponseFromSeatSeller = new ResponseListener() {
			@Override
			public void onResponse(Object objTicketDetails) {
				// TODO Auto-generated method stub
				ArrayList<CancellationTicketDetailsModel> cancelTicketDetailsList = (ArrayList<CancellationTicketDetailsModel>) objTicketDetails;
				System.out.println("Ticket Details:" + objTicketDetails);

				setValueInView(cancelTicketDetailsList);

			}
		};

		// TODO Auto-generated method stub
		BusTicketManagaer busticketManager = new BusTicketManagaer();
		busticketManager.getCancellationTicketDetails(
				CancelledSeatActivity.this,
				ticketDetailsResponseFromSeatSeller, AppData.CONSUMERKEY,
				AppData.SECRETKEY, AppData.BASESEATURL
						+ "/cancellationdata?tin=" + strTicketNo,
				"TicketDetails");
	}

	private void setValueInView(
			ArrayList<CancellationTicketDetailsModel> cancelTicketDetailsList) {
		// TODO Auto-generated method stub
		strPartialcallationAllow = getIntent().getBooleanExtra(
				"PartialCancellationAllow", true);
		strDOJ = getIntent().getStringExtra("StrDOJ");
		strcancellationPolicy = getIntent()
				.getStringExtra("cancellationPolicy");
		scrlLayout.setVisibility(View.VISIBLE);
		llPassengerCancelCharge.setVisibility(View.VISIBLE);
		strTicketNo = getIntent().getStringExtra("TicketNo");
		tvNewPNRNo.setText(getIntent().getStringExtra("PNR no"));
		tvStatus.setText(getIntent().getStringExtra("status"));
		tvTotalFare.setText(getIntent().getStringExtra("TotalFare"));
		tvDepartureTime.setText(getIntent().getStringExtra("DepartureTime"));
		tvTravelsName.setText(getIntent().getStringExtra("TravelsName"));
		tvTravelContactNo.setText(getIntent().getStringExtra(
				"TravellerContactNo"));
		tvToValue.setText(getIntent().getStringExtra("SourceName"));
		tvFromValue.setText(getIntent().getStringExtra("DestinationValue"));
		tvTicketNo.setText(getIntent().getStringExtra("TicketNo"));
		btn_year.setText(getIntent().getStringExtra("YearButton"));
		btn_date.setText(getIntent().getStringExtra("dateButton"));
		btn_dayName.setText(getIntent().getStringExtra("dayNameButtom"));
		addDynamicPassenger(inventoryList, cancelTicketDetailsList);

		chkPassenger.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				checkedaddstate(isChecked);
			}

		});
	}

	protected void checkedaddstate(boolean isChecked) {
		// TODO Auto-generated method stub
		if (isChecked) {
			for (int i = 0; i < lstChekbox.size(); i++) {
				Log.d("XCcsaskjdfsdjk", "in checkbox");
				lstChekbox.get(i).setChecked(true);
			}
		} else {
			if (cancellationChargeList.size() == inventoryList.size()) {
				for (int i = 0; i < lstChekbox.size(); i++) {
					Log.d("XCcsaskjdfsdjk", "in checkbox");
					lstChekbox.get(i).setChecked(false);
				}
			}
		}
	}

	private void addDynamicPassenger(
			final ArrayList<TicketDetailsModel> inventoryList,
			final ArrayList<CancellationTicketDetailsModel> cancelTicketDetailsList) {
		// TODO Auto-generated method stub
		for (int i = 0; i < inventoryList.size(); i++) {
			llPassengerMain = new LinearLayout(this);
			llPassengerMain.setOrientation(LinearLayout.HORIZONTAL);
			LayoutParams linLayoutParam = new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			// set LinearLayout as a root element of the screen
			llPassengerMain.setLayoutParams(linLayoutParam);
			LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);

			chkPassenger = new CheckBox(CancelledSeatActivity.this);
			chkPassenger.setLayoutParams(lpView);
			chkPassenger.setButtonDrawable(getResources().getDrawable(
					R.drawable.ckb_bg_selector_cancel));
			llPassengerMain.addView(chkPassenger);
			chkPassenger.setId(i);
			lstChekbox.add(chkPassenger);
			LinearLayout llpassengerdynamic = new LinearLayout(
					CancelledSeatActivity.this);
			llpassengerdynamic.setOrientation(LinearLayout.VERTICAL);
			LinearLayout.LayoutParams llpassngrmainparams = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);

			final TextView tvpassngrname = new TextView(
					CancelledSeatActivity.this);
			final TextView tvgenderAndAge = new TextView(
					CancelledSeatActivity.this);
			final TextView tvseatname = new TextView(CancelledSeatActivity.this);
			final TextView tvContactNo = new TextView(
					CancelledSeatActivity.this);
			final TextView tvPassengertitle = new TextView(
					CancelledSeatActivity.this);

			tvpassngrname.setText(inventoryList.get(i).gettravellerName());
			tvpassngrname.setTextSize(15);
			tvpassngrname.setTypeface(Typeface.DEFAULT_BOLD);
			tvpassngrname.setTextColor(getResources().getColor(R.color.black));

			tvgenderAndAge.setText(inventoryList.get(i).getage() + " "
					+ inventoryList.get(i).getgender());
			tvgenderAndAge.setTextSize(15);
			tvgenderAndAge.setTextColor(getResources().getColor(R.color.black));

			tvseatname.setText(getResources().getString(R.string.seat_name)
					+ " : " + inventoryList.get(i).getTDseatName());
			tvseatname.setTextColor(getResources().getColor(R.color.black));
			tvseatname.setTextSize(15);

			tvContactNo.setText(inventoryList.get(i).getphno());
			tvContactNo.setTextSize(13);
			tvPassengertitle.setText(inventoryList.get(i).getTitle());
			final View view = new View(CancelledSeatActivity.this);
			view.setLayoutParams(new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT, 2));
			view.setBackgroundColor(getResources()
					.getColor(R.color.view_margin));
			final TextView tvCharge = new TextView(CancelledSeatActivity.this);
			final TextView tvRefund = new TextView(CancelledSeatActivity.this);

			tvCharge.setText(cancelTicketDetailsList.get(i)
					.getCancelTicketList().get(i).getvalue());

			tvCharge.setTextSize(15);
			tvRefund.setText(cancelTicketDetailsList.get(i).getfareList()
					.get(i).getfareValue());

			tvRefund.setTextSize(15);
			llpassngrmainparams.setMargins(0, 5, 0, 0);
			LinearLayout.LayoutParams llpassngrmainparamsname = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			llpassngrmainparamsname.setMargins(0, 0, 0, 5);
			llpassengerdynamic.addView(tvpassngrname, llpassngrmainparams);
			llpassengerdynamic.addView(tvgenderAndAge);
			llpassengerdynamic.addView(tvseatname, llpassngrmainparamsname);

			llPassengerMain.addView(llpassengerdynamic);

			if (cancelTicketDetailsList.get(i).getpartiallyCancellable() == false) {
				chkPassenger.setChecked(true);
				chkPassenger.setEnabled(false);
				if (!cancelTicketDetailsList.get(i).getCancelTicketList()
						.isEmpty()) {
					checkedseatadded(cancelTicketDetailsList, tvpassngrname,
							tvgenderAndAge, tvPassengertitle, tvCharge,
							tvRefund, tvseatname, true);
				}

			}
			chkPassenger
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (isChecked) {
								onClickCheckBox(cancelTicketDetailsList,
										tvpassngrname, tvgenderAndAge,
										tvPassengertitle, tvCharge, tvRefund,
										tvseatname, isChecked);
							}
						}
					});
			llRoot.addView(llPassengerMain);

		}
	}

	protected void onClickCheckBox(
			ArrayList<CancellationTicketDetailsModel> cancelTicketDetailsList,
			TextView tvpassngrname, TextView tvgenderAndAge,
			TextView tvPassengertitle, TextView tvCharge, TextView tvRefund,
			TextView tvseatname, boolean isChecked) {
		// TODO Auto-generated method stub
		if (isChecked == true) {
			cancellationChargeList.add(tvCharge.getText().toString());
			refundFareList.add(tvRefund.getText().toString());
			llPassengerCancelCharge.setVisibility(View.VISIBLE);
			passengerDetailsList.add(tvpassngrname.getText().toString() + ";"
					+ tvgenderAndAge.getText().toString() + " | "
					+ tvseatname.getText().toString() + ";"
					+ tvPassengertitle.getText().toString());

		} else {
			passengerDetailsList.remove(tvpassngrname.getText().toString()
					+ ";" + tvgenderAndAge.getText().toString() + " | "
					+ tvseatname.getText().toString() + ";"
					+ tvPassengertitle.getText().toString());

			cancellationChargeList.remove(tvCharge.getText().toString());
			refundFareList.remove(tvRefund.getText().toString());
			llPassengerCancelCharge.setVisibility(View.VISIBLE);
		}
		if (!cancellationChargeList.isEmpty()) {
			double cancelCharg = 0.0, cancelFare = 0.0;
			for (int i = 0; i < cancellationChargeList.size(); i++) {
				cancelCharg = cancelCharg
						+ Double.parseDouble(cancellationChargeList.get(i));
				cancelFare = cancelFare
						+ Double.parseDouble(refundFareList.get(i));
				tvCancellationCharge.setText("INR" + " " + cancelCharg);
				tvRefundAmt.setText("INR" + " " + ""
						+ (cancelFare - cancelCharg));
			}
		}

		if (cancellationChargeList.size() == inventoryList.size()) {
			chkPassenger.setChecked(true);
		} else {
			chkPassenger.setChecked(false);
		}
	}

	private void checkedseatadded(
			ArrayList<CancellationTicketDetailsModel> cancelTicketDetailsList,
			TextView tvpassngrname, TextView tvgenderAndAge,
			TextView tvPassengertitle, TextView tvCharge, TextView tvRefund,
			TextView tvseatname, boolean ischeked) {
		// TODO Auto-generated method stub
		if (ischeked) {
			passengerDetailsList.add(tvpassngrname.getText().toString() + ";"
					+ tvgenderAndAge.getText().toString() + " | "
					+ tvseatname.getText().toString() + ";"
					+ tvPassengertitle.getText().toString());
			cancellationChargeList.add(tvCharge.getText().toString());
			refundFareList.add(tvRefund.getText().toString());
		} else {
			passengerDetailsList.remove(tvpassngrname.getText().toString()
					+ ";" + tvgenderAndAge.getText().toString() + " | "
					+ tvseatname.getText().toString() + ";"
					+ tvPassengertitle.getText().toString());
			cancellationChargeList.remove(tvCharge.getText().toString());
			refundFareList.remove(tvRefund.getText().toString());
		}
		if (!cancellationChargeList.isEmpty()) {
			double cancelCharg = 0.0, cancelFare = 0.0;
			for (int i = 0; i < cancellationChargeList.size(); i++) {
				cancelCharg = cancelCharg
						+ Double.parseDouble(cancellationChargeList.get(i));
				cancelFare = cancelFare
						+ Double.parseDouble(refundFareList.get(i));
				tvCancellationCharge.setText("INR" + " " + cancelCharg);
				tvRefundAmt.setText("INR" + " " + ""
						+ (cancelFare - cancelCharg));
			}
		}
		if (cancellationChargeList.size() == inventoryList.size()) {
			chkPassenger.setChecked(true);
		} else {
			chkPassenger.setChecked(false);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_headerBack:
			finish();
			break;
		case R.id.btn_cancel:
			onClickCancelseat();
			break;
		case R.id.tvCancellationPOlicy:
			onClickCancellationPolicy();
			break;
		default:
			break;
		}
	}

	private void onClickCancellationPolicy() {
		// TODO Auto-generated method stub
		CancellationPolicy cancellationPolicyDialog = new CancellationPolicy(
				CancelledSeatActivity.this, tvDepartureTime.getText()
						.toString().trim(), strcancellationPolicy, strDOJ,
				strPartialcallationAllow);
		cancellationPolicyDialog.show();
	}

	private void onClickCancelseat() {
		// TODO Auto-generated method stub
		Intent cancelSeatIntent = new Intent(CancelledSeatActivity.this,
				CancelSeatPinActivity.class);
		cancelSeatIntent.putExtra("TicketNo", strTicketNo);
		cancelSeatIntent.putExtra("sourceValue", tvFromValue.getText()
				.toString());
		cancelSeatIntent.putExtra("DestinationValue", tvToValue.getText()
				.toString());
		cancelSeatIntent.putExtra("trvelsName", tvTravelsName.getText()
				.toString());
		cancelSeatIntent.putExtra("DOJ", tvDepartureTime.getText().toString());
		cancelSeatIntent.putStringArrayListExtra("PassengerDetails",
				passengerDetailsList);
		cancelSeatIntent.putStringArrayListExtra("cancellationChargeList",
				cancellationChargeList);
		cancelSeatIntent.putExtra("RefundList", refundFareList);
		cancelSeatIntent.putExtra("PNR", tvNewPNRNo.getText().toString());
		cancelSeatIntent.putExtra("Status", tvStatus.getText().toString());

		startActivity(cancelSeatIntent);
	}
}
